package com.hanyxx.thread.cinema;

import lombok.SneakyThrows;
/**
 * @author layman
 */
public class Demo01 {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        ThreadA t1 = new ThreadA(lock);
        ThreadA t2 = new ThreadA(lock);
        System.out.println("t1的线程状态：" + t1.getState());
        t1.start();
        System.out.println("t1的线程状态：" + t1.getState());
        //确保t1线程进入睡眠
        Thread.sleep(500L);
        System.out.println("t1的线程状态：" + t1.getState());
        t2.start();
       //t2的线程状态：RUNNABLE? 为什么不是BLOCKED?奇怪?
        System.out.println("t2的线程状态：" + t2.getState());
        //确保t1线程执行完毕
        Thread.sleep(5000L);
        System.out.println("t1的线程状态：" + t1.getState());

        ThreadB t3 = new ThreadB();
        t3.start();
        Thread.sleep(100L);
        System.out.println("t3的线程状态：" + t3.getState());
    }
}
class ThreadA extends Thread{
    private Object lock;
    public ThreadA(Object lock){
        this.lock = lock;
    }
    @SneakyThrows
    @Override
    public void run() {

        synchronized (lock){
            System.out.println(Thread.currentThread().getName()+"---");
            Thread.sleep(3000L);
        }
    }
}
class ThreadB extends Thread{
    private Object lock = new Object();

    @SneakyThrows
    @Override
    public void run() {
        synchronized (lock){
            lock.wait();
        }
    }
}
